﻿// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="ExportListItem.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Defines the ExportListItem type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------
namespace Collaboris.Saf.Actions.Wss.ContentMigration
{
    using System.Collections.Generic;
    using System.Diagnostics;

    using Engine;

    using Microsoft.SharePoint;

    using SharePoint.Wss.Attributes;
    using SharePoint.Wss.ContentMigration;
    using SharePoint.Wss.Lists;
    using SharePoint.Wss.Webs;

    using Utils;

    [CollaborisClass(
        Title =
            "Exports a List Item to a CMP.",
        ReleaseStatus =
            ReleaseStatus.Stable,
        DisableDocumentation =
            false,
        ContactAuthorEmail =
            "Support@Collaboris.co.uk",
        Summary =
            "Exports a SharePoint list item to a Content Migration Package (CMP) based on the web, list name " + 
            "and list item ID.",
        Remarks =
            "<p>The 'Do' method for this action uses the standard SharePoint content migration " +
            "API to achieve the export. " +
            "It is highly recommended that the server is running a minimum of SharePoint " +
            "Service Pack 1.</p>" +
            @"This action can easily be paired with <a href=""C:\Temp\safDoc\Collaboris.Saf.Actions.Wss.ContentMigration.ImportListItem.html"">Import List Item</a>" +
            " to move content between sites and/or farms.",
        ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C634}")]
    public class ExportListItem : WSSActionBase<ListItemExportSettings>
    {
        #region Fields (1)

        ContentMigrationHelper helper = new ContentMigrationHelper();

        #endregion Fields

        #region Methods (7)

        /// <summary>
        /// When called causes the list passed in to be exported to a 
        /// CMP Package to a specified file.
        /// </summary>
        /// <param name="context">
        /// </param>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <returns>
        /// </returns>
        protected override ProcessStatus DoAction(SafContext context, ListItemExportSettings entity)
        {
            this.Trace.TraceMethodStart("DoAction");

            SPList list = SPListHelper.GetList(TargetWeb, entity.ListName);

            // TODO: Add Tracing
            SPListItem item = SPListItemHelper.GetListItem(TargetWeb, list, entity.ListItemId);

            ValidationHelper.VerifyObjectArgument(item, "item");
            helper.ExportListItem(entity.ExportSettings, item, TargetWeb.Url);
           

            this.Trace.TraceMethodEnd("DoAction");

            return ProcessStatus.Success;
        }
       
        /// <summary>
        /// Checks the Data entity for Validity
        /// </summary>
        /// <param name="context">The <see cref="IContext"/>context.</param>
        /// <param name="entity">The data entity to Validate</param>
        /// <param name="errors">List of <see cref="ValidationError"/>s</param>
        /// <param name="mode"><see cref="ProcessMode"/> signalling wheter we are Doing, or Undoing the action.</param>
        /// <returns>false if invalid</returns>
        protected override bool IsValid(SafContext context, ListItemExportSettings entity, ref List<ValidationError> errors, ProcessMode mode)
        {
            int currentCount = errors.Count;

            // Validate each object now.
            ValidateStringForNull(entity.ListName, "ListItemExportSettings.ListName", ref errors);
            ValidateIntForZero(entity.ListItemId, "ListItemExportSettings.ListItemId", ref errors);
            
            

            ValidateObjectForNull(entity.ExportSettings, "ListItemExportSettings.ExportSettings", ref errors);
            if (entity.ExportSettings != null)
            {
                ValidateStringForNull(entity.ExportSettings.BaseFileName, "ListItemExportSettings.ExportSettings.BaseFileName", ref errors);
                ValidateStringForNull(entity.ExportSettings.FileLocation, "ListItemExportSettings.ExportSettings.FileLocation", ref errors);
            }           
            
            // Have we added errors ?
            return errors.Count == currentCount;
        }
        
        #endregion Methods
    }
}